/**
	Author : behrooz
	Date : Nov 12, 2011
	Project : Curve Descriptor
 **/
package edu.oregonstate.vision;

import java.util.ArrayList;
import java.util.List;

import edu.oregonstate.vision.common.EuclideanDistanceCalculator;
import edu.oregonstate.vision.common.Point;
import edu.oregonstate.vision.common.TanjentOrientationCalculator;

public class RunDescriptorProducer {
	private static int averageNumberOfPoints(List<Track> tracks) {
		int average = 0;
		int maxNumberOfPoints = 0;
		for (Track track : tracks) {
			if (maxNumberOfPoints < track.getPoints().size())
				maxNumberOfPoints = track.getPoints().size();
			average += track.getPoints().size();
		}
		System.out.println("Max Number Of Points:" + maxNumberOfPoints);
		return average / tracks.size();
	}

	private static void unifyTrackPoints(List<Track> tracks, int numberOfPoints) {
		for (Track track : tracks) {
			List<Point> points = track.getPoints();
			List<Point> newPoints = new ArrayList<Point>();
			int step = points.size() / numberOfPoints;
			for (int i = 0; step != 0 && newPoints.size() < numberOfPoints
					&& i < points.size(); i += step) {
				newPoints.add(points.get(i));
			}
			track.setPoints(newPoints);
			// System.out.println(track.getName() + ":[Old #Points: "
			// + points.size() + "_ New #Points: "
			// + track.getPoints().size() + "]");
		}
	}

	public static void main(String[] args) {
		List<Track> tracks = new TrackReader().parseTracks("tracks_video1.txt");
		int averagePointNumbers = averageNumberOfPoints(tracks);
		unifyTrackPoints(tracks, 10);
		for (Track track : tracks) {
			List<Point> points = track.getPoints();
			if (points.size() == 0)
				continue;
			EuclideanDistanceCalculator.getInstance().calculate(points, points)
					.writeToFile(track.getName() + "_distanceDS.txt");
			TanjentOrientationCalculator.getInstance().calculate(points)
					.writeToFile(track.getName() + "_angleDS.txt");
		}
		if (MatlabFacade.getMatlabProxy() != null) {
			MatlabFacade.getMatlabProxy().disconnect();
		}
	}
}
